/*
 *
 *  AutoWallpaper (Android) Version 1.x
 *  Copyright (C) 2010 - 2011, Tyler H. Jones (me@tylerjones.me)
 *  http://www.autowallpaper.org/
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */


package com.tylerhjones.autowallpaper;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

public class Database {
	private String TAG = "Database";
	private SQLiteDatabase theDB;
	private Context context;
	private DBHelper dbhelper;
	
	public Database(Context c) {
		Log.v(TAG, "Database class cunstructor called...");
		this.context = c;
	}
	
	//******************************************************************************
	// Open & CLose the DB
	
	public Database open() throws SQLiteException {
		try {
			Log.v(TAG, "Opening database...");
			dbhelper = new DBHelper(context);
			theDB = dbhelper.getWritableDatabase();
			return this;
		} catch(SQLiteException e) {
			Log.e(TAG, "Database.Open() threw an exception!", e);
			return null;
		}
	}

	public void close() {
		try {
			dbhelper.close();
		} catch(SQLiteException e) {
			Log.e(TAG, "Database.Close() threw an exception!", e);
		}
	}
	
	// END -  Open & CLose the DB
	//******************************************************************************

	public long addImage(String img) {
		try {
			Log.v(TAG, "addImage()");
			if(img.trim() == "" || img == null) { return -1; }
			ContentValues values = new ContentValues();
			values.put(Common.S_FIELD_PATH, img);
			values.put(Common.S_FIELD_TIMESVIEWED, "0");
			values.put(Common.S_FIELD_LASTVIEWED, String.valueOf(System.currentTimeMillis()));
			return theDB.insert(Common.TABLE_IMAGES, null, values);
		} catch(SQLiteException e) {
			Log.e(TAG, "addImage() threw an exception!", e);
			return -1;
		}
	}
	
	public int incrementViews(String path) {
		Log.v(TAG, "incrementViews()");
		Cursor mCursor = theDB.rawQuery("UPDATE " + Common.TABLE_IMAGES + " SET(" + Common.S_FIELD_TIMESVIEWED + "=" + Common.S_FIELD_TIMESVIEWED + "+1) WHERE " + Common.S_FIELD_PATH + "=" + path, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor.getInt(0);
	}

	public boolean deleteImage(String path) {
		Log.v(TAG, "deleteImage()");
		return theDB.delete(Common.TABLE_IMAGES, Common.S_FIELD_PATH + "='" + path + "'", null) > 0;
	}
	
	public boolean deleteAllImages() {
		Log.v(TAG, "deleteAllImages()");
		return theDB.delete(Common.TABLE_IMAGES, Common.S_FIELD_INDEX + "> -1", null) > 0;
	}
	
	public Cursor Images() {		
		try {
			Log.v(TAG, "Images()");
			Cursor curs = theDB.query(Common.TABLE_IMAGES, new String[] { Common.S_FIELD_PATH, Common.S_FIELD_TIMESVIEWED, Common.S_FIELD_LASTVIEWED, Common.S_FIELD_INDEX }, null, null, null, null, null);
			return curs;
		} catch(SQLiteException e) {
			Log.e(TAG, "Images() threw an exception!", e);
			return null;
		}
	}
	
	public int NumImages() {	
		try {
			Log.v(TAG, "NumImages()");
			String sql = "SELECT COUNT(*) FROM " + Common.TABLE_IMAGES;
		    SQLiteStatement statement = theDB.compileStatement(sql);
		    long num = statement.simpleQueryForLong();
			Log.v(TAG, "NumImages() returned: " + String.valueOf(num));
			return (int) num;
		} catch(SQLiteException e) {
			Log.e(TAG, "NumImages() threw an exception!", e);
			return 0;
		}
	}

	public Cursor getImageFromIndex(int idx) throws SQLiteException {
		Log.v(TAG, "getImageFromIndex()");
		Cursor mCursor = theDB.query(true, Common.TABLE_IMAGES, new String[] { Common.S_FIELD_PATH, Common.S_FIELD_TIMESVIEWED, Common.S_FIELD_LASTVIEWED }, Common.S_FIELD_INDEX + "=" + idx, null, null, null, null, null);
		if (mCursor != null) { mCursor.moveToFirst(); }
		return mCursor;
	}
	
	public Cursor getImageFromPath(String path) throws SQLiteException {
		Log.v(TAG, "getImageFromPath()");
		Cursor mCursor = theDB.query(true, Common.TABLE_IMAGES, new String[] { Common.S_FIELD_INDEX, Common.S_FIELD_TIMESVIEWED, Common.S_FIELD_LASTVIEWED }, Common.S_FIELD_PATH + "='" + path + "'", null, null, null, null, null);
		if (mCursor != null) { mCursor.moveToFirst(); }
		return mCursor;
	}
	
	public boolean ImageExists(String i) {
		Cursor c = getImageFromPath(i);
		if(c.getCount() > 0) { return true; }
		return false;
	}
}
